﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ComputeShaderScript : MonoBehaviour
{
    public Dictionary<Vector2Int, float> cells;
    private ComputeBuffer buffer = null;
    private List<CellBufferElement> bufferElements;


    /// <summary>
    /// The compute shader to use for rendering the mask
    /// </summary>
    [SerializeField]
    private ComputeShader computeShader = null;

    /// <summary>
    /// The size the mask should have
    /// Idealy this is a power of two
    /// </summary>
    [Range(64, 4096)]
    //[SerializeField]
    private int TextureSize = 1024;

    private RenderTexture maskTexture;
    private static readonly int maskTextureId = Shader.PropertyToID("_Result");
    private static readonly int cellBufferId = Shader.PropertyToID("_CellBuffer");

    private struct CellBufferElement
    {
        public float PositionX;
        public float PositionY;
        public float Visibility;
    }

    private void Awake()
    {
        //Create a new render texture for the mask
        maskTexture = new RenderTexture(TextureSize, TextureSize, 0, RenderTextureFormat.ARGB32)
        {
            enableRandomWrite = true
        };
        maskTexture.Create();

        computeShader.SetTexture(0, maskTextureId, maskTexture);

        Shader.SetGlobalTexture(maskTextureId, maskTexture);

        //创建保存的cell
        cells = new Dictionary<Vector2Int, float>();
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                cells.Add(new Vector2Int(i, j), 0);
            }
        }
        bufferElements = new List<CellBufferElement>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            var pos = GetScreenToYAxis(Input.mousePosition);
            var touch = new Vector2Int(Mathf.FloorToInt(pos.x), Mathf.FloorToInt(pos.z));
            if (cells.ContainsKey(touch))
            {
                cells[touch] = 1 - cells[touch];
                Debug.Log(cells[touch]);
            }
        }

        bufferElements.Clear();
        foreach (var cell in cells)
        {
            CellBufferElement element = new CellBufferElement
            {
                PositionX = cell.Key.x + 0.5f,
                PositionY = cell.Key.y + 0.5f,
                Visibility = cell.Value
            };

            //if(cell.Value == 1)
            //{
            //    Debug.Log(
            //}

            bufferElements.Add(element);
        }

        if (buffer == null)
            buffer = new ComputeBuffer(cells.Count * 3, sizeof(float));


        buffer.SetData(bufferElements);
        computeShader.SetBuffer(0, cellBufferId, buffer);


        computeShader.Dispatch(0, Mathf.CeilToInt(TextureSize / 8.0f), Mathf.CeilToInt(TextureSize / 8.0f), 1);
    }

    Vector3 GetScreenToYAxis(Vector3 screenPos)
    {
        var ray = Camera.main.ScreenPointToRay(screenPos);
        var num = (0 - ray.origin.y) / ray.direction.y;
        return ray.origin + ray.direction * num;
    }

    private void OnDestroy()
    {
        buffer?.Dispose();
    }
}
